package com.ftm.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ftm.entity.Shop;
import com.ftm.entity.Shopc;
import com.ftm.service.ShopCarService;
import com.ftm.service.ShopService;
import com.ftm.service.impl.ShopCarServiceImpl;
import com.ftm.service.impl.ShopServiceImpl;

/**
 * Servlet implementation class GoodsDetailsService
 */
@WebServlet("/GoodsDetailsService")
public class GoodsDetailsService extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GoodsDetailsService() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at: ").append(request.getContextPath());
		// 1设置请求编码
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("addShopCar".equals(op)) {
			addShopCar(request, response);
		}
		System.out.println(op);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

	private void addShopCar(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(request.getSession().getAttribute("user")==null) {
			request.getRequestDispatcher("UserLogin.jsp").forward(request, response);
		}else {
			String imgname = request.getParameter("img_name");
			String goodsname = request.getParameter("goods_name");
			String goodsprice = request.getParameter("goods_price");
			String shopname = request.getParameter("goods_Shopname");
			String goodscount = request.getParameter("goods_num");
			int num = Integer.parseInt(goodscount);
			String userid = request.getSession().getAttribute("user") + "";
			double total = num * (Double.parseDouble(goodsprice));
			int user = Integer.parseInt(userid);
			ShopCarService scs = new ShopCarServiceImpl();
			ShopService ss = new ShopServiceImpl();
			List<Shop> list = ss.selShopname(shopname);
			int shopid = 0;
			if (list.size() > 0) {
				for (Shop shop : list) {
					shopid = shop.getShopid();
				}
			}
			Shopc shopc = new Shopc(user, shopid, goodsname, num, total);
			boolean flag= scs.addGoods(shopc);
			if(flag) {
				request.getRequestDispatcher("Cart.jsp").forward(request, response);
			}else {
				response.getWriter().append(
						"<script>alert(\"添加失败\");location.href='http://localhost:8080/ForTheMorning/Goods_details.jsp';</script>");
			}
			System.out.println("图片：" + imgname + "-商品名：" + goodsname + "-单价：" + goodsprice + "-店名：" + shopname + "-数量："
					+ goodscount + "-用户：" + userid);
		}
	}
}
